package top.rainf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.rainf.domain.vo.ClassInfo;
import top.rainf.domain.vo.Post;
import top.rainf.domain.vo.UserBaseInfo;
import top.rainf.service.IBlockService;
import top.rainf.service.IClassInfoService;
import top.rainf.service.IPostService;
import top.rainf.support.Result;
import top.rainf.domain.vo.Block;
import top.rainf.util.PagerUtil;

import java.util.List;

/**
 * @author 星环
 * @date 2023/5/10 11:20:36
 * @description 板块控制器
 */


@RestController
@RequestMapping("/block")
public class BlockController {

    @Autowired
    IBlockService blockService;

    @Autowired
    IClassInfoService classInfoService;

    @Autowired
    IPostService postService;

    @RequestMapping("/list")
    public Result list(){
        List<Block> list = blockService.getBlockList();
        return Result.success().addObj("list",list);
    }

    @RequestMapping("/one")
    public Result one(int id){
        Block b = blockService.getOne(id);
        return b == null ? Result.fail() : Result.success().addObj("block",b);
    }

    /**
     * 查询今天该版块的发帖数量
     * @param id
     * @return
     */
    @RequestMapping("/today")
    public Result todayPostCount(int id){
        int count = blockService.getTodayPostCount(id);
        return Result.success().addObj("today_count",count);
    }

    /**
     * 查询版主列表
     * @param id 板块的id
     * @return 版主列表
     */
    @RequestMapping("/block-admin-list")
    public Result getBlockAdminList(int id){
        List<UserBaseInfo> list = blockService.getBlockAdminList(id);
        return Result.success().addObj("list",list);
    }

    /**
     * 要初始化一个板块详情页的全部信息，何必从每个路径分别请求？直接请求这个接口就行。
     * @param id 哪个板块 id
     * @return 所有数据
     */
    @RequestMapping("init-page")
    public Result initPage(int id){
        //获取一个板块的基本数据
        Block block = blockService.getOne(id);
        //获取版主列表
        List<UserBaseInfo> blockAdminList = blockService.getBlockAdminList(id);
        //获得今天发帖数
        int todayPostCount = blockService.getTodayPostCount(id);
        //所有分类信息
        List<ClassInfo> classInfoList = classInfoService.getClassInfoListByBlockId(id);
        //本板块所有帖子的第一页，10条
        //默认根据发帖时间排序，其实可以按照最后回复时间排序的，正常的逻辑
        List<Post> postList = postService.getAllPostListPagedByBlockId(id,10,1);
        //这是后端完成分页器每个选项的内容，前端直接放进右下角的分页作按钮就行。
        List<String> pageList = PagerUtil.getPageListStr(10,1,postService.getTotalCountInBlock(id));
        return Result.success().addObj("block",block)
                .addObj("blockAdminList",blockAdminList)
                .addObj("todayPostCount",todayPostCount)
                .addObj("classInfoList",classInfoList)
                .addObj("postList",postList)
                .addObj("pageList",pageList);
    }




}
